package com.yoga.api;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yoga.model.Reservation;
import com.yoga.service.ReservationService;
import com.yoga.utils.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

/**
 * @author yan
 * @date 2025/8/17
 */
@RestController
@RequestMapping(value = "/api/v1/reservation", produces = MediaType.APPLICATION_JSON_VALUE)
public class ReservationApi {
    private ReservationService reservationService;
    @Autowired
    public void setReservationService(ReservationService reservationService) {
        this.reservationService = reservationService;
    }

    @GetMapping
    public ResponseEntity<JsonResult<?>> findAll(@RequestParam(defaultValue = "1") Integer pageNo,
                                                 @RequestParam(defaultValue = "10") Integer pageSize, Reservation reservation){
        Page<Reservation> page = new Page<>(pageNo,pageSize);
        page = this.reservationService.findAll(page,reservation);
        return ResponseEntity.ok(JsonResult.success(page));
    }

    @DeleteMapping
    public ResponseEntity<JsonResult<?>> deleteByIds(@RequestBody Integer[] ids){
        int count = this.reservationService.deleteByIds(ids);
        if (count == 0){
            return ResponseEntity.ok(JsonResult.fail("删除失败"));
        }else {
            return ResponseEntity.ok(JsonResult.success(count));
        }
    }

    @PostMapping
    public ResponseEntity<JsonResult<?>> add(@RequestBody Reservation reservation){
        boolean flag = this.reservationService.add(reservation);
        if (flag){
            return ResponseEntity.ok(JsonResult.success("添加成功"));
        }else {
            return ResponseEntity.ok(JsonResult.fail("添加失败"));
        }
    }

    @PutMapping
    public ResponseEntity<JsonResult<?>> update(@RequestBody Reservation reservation){
        boolean flag = this.reservationService.update(reservation);
        if (flag){
            return ResponseEntity.ok(JsonResult.success("修改成功"));
        }else {
            return ResponseEntity.ok(JsonResult.fail("修改失败"));
        }
    }
}
